<template>
  <div class="ezsCaseComp">
    <div class="ezsCaseCompTitle fontColorTheme">编辑其他信息</div>
    <el-form :model="info" :rules="rules" ref="ruleForm" label-width="140px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="所在地区" prop="areaId" v-if="isEzs">
            <el-cascader
              class="caseCompTitleInput ezsCaseCompTitleInput"
              ref="manageArea"
              v-model="taskAreaId"
              :options="areasOptions"
              @change="getTaskAreaId"
              popper-class="ezCascaderStyle"
              clearable
              filterable
              :props="{ checkStrictly: true }"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="所在地区" prop="areaId" v-else>
            <el-cascader
              class="caseCompTitleInput ezsCaseCompTitleInput"
              ref="manageArea"
              v-model="taskAreaId"
              :options="areasOptions"
              @change="getTaskAreaId"
              clearable
              filterable
              :props="{ checkStrictly: true }"
            ></el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="欠薪人数" prop="backPayCount">
            <el-input
              v-model="info.backPayCount"
              clearable=""
              placeholder="请输入"
              class="caseCompTitleInput ezsCaseCompTitleInput"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="欠薪金额" prop="backPayTotal">
            <el-input
              v-model="info.backPayTotal"
              clearable=""
              placeholder="请输入"
              class="caseCompTitleInput ezsCaseCompTitleInput"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="欠薪始发时间" prop="involveCount" width="100%">
            <el-date-picker
              type="month"
              value-format="yyyy-MM"
              @change="handleDate"
              class="caseCompTitleInput ezsCaseCompTitleInput"
              placeholder="选择日期"
              v-model="involveCount"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="是否曾经通过以下途径反映上述问题："
            class="singleLabel"
            prop="onceReflected"
            v-if="isHsUser() && activeName == 'PLAT'"
          >
            <!-- <el-select v-model="info.onceReflected"  class="caseCompTitleInput ezsCaseCompTitleInput" placeholder="请选择">
                        <el-option v-for="item in resolveStatus" :key="item.value" :label="item.name" :value="item.value"></el-option>
                    </el-select> -->
            <el-input
              v-model="info.onceReflected"
              clearable=""
              placeholder="请输入"
              class="caseCompTitleInput ezsCaseCompTitleInput"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="对曾经反映情况进行详细说明："
            class="singleLabel"
            prop="detailedDescription"
            v-if="isHsUser() && activeName == 'PLAT'"
          >
            <el-input
              v-model="info.detailedDescription"
              clearable=""
              placeholder="请输入"
              class="caseCompTitleInput ezsCaseCompTitleInput"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="ezsCaseCompBtn">
      <el-button type="primary" class="commonBut saveBtn" @click="save"
        >保存</el-button
      >
      <el-button type="default" class="commonBut" @click="finish"
        >取消</el-button
      >
    </div>
  </div>
</template>
<script>
import { mapActions, mapState } from 'vuex'

export default {
  name: 'caseDepartment',
  props: {
    childData: {
      type: Object,
      required: true,
      default() {
        return {}
      },
    },
  },
  data() {
    var checkMoney = (rule, value, callback) => {
      if (!value) {
        callback()
      } else {
        var rePayTotal =
          /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/
        if (!rePayTotal.test(value) || value == 0.0 || value == 0.0) {
          callback(new Error('金额格式不正确,必须大于0,最多带有2位小数'))
        } else {
          if (value >= 100000000000) {
            callback(new Error('金额需小于1000亿'))
          } else {
            callback()
          }
        }
      }
    }
    return {
      taskAreaId: [],
      involveCount: '',
      resolveStatus: [
        { value: '是', name: '是' },
        { value: '否', name: '否' },
      ],
      activeName: '',
      info: {
        id: '',
        areaId: '',
        areaName: '',
        cityId: '',
        cityName: '',
        countyId: '',
        countyName: '',

        complaintYear: '',
        complaintMonth: '',
        backPayCount: '',
        backPayTotal: '',
      },
      rules: {
        areaId: [
          { required: true, message: '请选择解决区域', trigger: 'change' },
        ],
        backPayCount: [
          {
            pattern: /^[1-9]+[0-9]*$/,
            message: '请输入正整数',
            trigger: 'blur',
          },
        ],
        backPayTotal: [{ validator: checkMoney, trigger: 'blur' }],
      },
      initInfo: {},
      isEzs: false,
    }
  },
  computed: { ...mapState(['areasOptions']) },
  mounted() {
    this.activeName = this.$route.query.activeName
    var currentAreaId = this.$store.state.currentAreaId
    if (currentAreaId) {
      if (currentAreaId === '4207' || currentAreaId.indexOf('4207') > -1) {
        this.isEzs = true
      } else {
        this.isEzs = false
      }
    }
    this.getAreas({ areaId: this.$store.state.areaInfoArr.areaId })
    const {
      id,
      areaId,
      areaName,
      complaintYear,
      complaintMonth,
      backPayCount,
      backPayTotal,
      streetId,
      streetName,
      onceReflected,
      detailedDescription,
    } = this.childData
    this.taskAreaId = areaId
    if (complaintYear) {
      this.involveCount = complaintYear + '-' + complaintMonth
    }
    if (this.activeName == 'PLAT') {
      this.info = {
        id,
        areaId,
        streetId,
        streetName,
        areaName,
        backPayCount,
        backPayTotal,
        complaintYear,
        complaintMonth,
        onceReflected,
        detailedDescription,
      }
    } else {
      this.info = {
        id,
        areaId,
        streetId,
        streetName,
        areaName,
        backPayCount,
        backPayTotal,
        complaintYear,
        complaintMonth,
      }
    }

    this.initInfo = JSON.parse(JSON.stringify(this.info))
    this.$refs.ruleForm.resetFields()
  },
  methods: {
    ...mapActions(['getAreas']),
    getTaskAreaId() {
      let result = this.$refs.manageArea.getCheckedNodes()[0]

      if (this.taskAreaId.length) {
        const { path, pathLabels } = result
        this.info.areaId = path[path.length - 1]
        this.info.areaName = pathLabels[pathLabels.length - 1]
      } else {
        this.info.areaId = ''
        this.info.areaName = ''
      }
    },
    handleDate(val) {
      if (val) {
        this.info.complaintYear = val.split('-')[0]
        this.info.complaintMonth = val.split('-')[1]
      } else {
        this.info.complaintYear = ''
        this.info.complaintMonth = ''
      }
    },
    save() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.isLoading = true
          let params = { id: this.info.id }
          for (let item in this.info) {
            params[item] = this.info[item]
          }
          params.streetName = params.areaName
          params.streetId = params.areaId
          this.axios
            .post('api-r/rightbackpay/expand/edit', params)
            .then((res) => {
              if (!res.data.hasError) {
                this.$message({
                  type: 'success',
                  message: res.data.msg,
                })
                this.isLoading = false
                this.finish(1)
              } else {
                this.$message({
                  type: 'warning',
                  message: res.data.errorMsg,
                })
                this.isLoading = false
              }
            })
        } else {
          return false
        }
      })
    },
    finish(index) {
      this.$emit('callback', { success: index == 1 ? true : false })
    },
  },
}
</script>
<style lang="less">
.ezsCaseComp {
  margin-top: 20px;
  border: 1px solid #dcdfe6;
  background-color: #f8f9fb;
  padding: 15px;
  &Title {
    color: #009588;
    font-size: 14px;
    line-height: 30px;
    margin-bottom: 12px;
  }
  &List {
    line-height: 36px;
    height: 36px;
    font-size: 14px;
    color: #444;
    margin-bottom: 12px;
    &Title {
      display: inline-block;
      width: 85px;
      text-align: right;
    }
    &Input {
      width: calc(100% - 95px);
    }
    .input {
      width: calc(100% - 170px);
    }
  }
  &Btn {
    text-align: center;
    margin: 24px 0 10px;
    .el-button {
      width: 120px;
    }
  }
  /deep/ .el-radio__input.is-checked + .el-radio__label {
    color: #009588 !important;
  }
  /deep/.el-radio__input.is-checked .el-radio__inner {
    background: #009588 !important;
    border-color: #009588 !important;
  }
}
.ezsCaseCompTitleInput {
  width: calc(100%);
}
.ezCascaderStyle {
  .el-cascader-menu:nth-of-type(1) .el-radio__inner {
    display: none !important;
  }
}
.singleLabel {
  .el-form-item__label {
    line-height: 18px !important;
  }
}
</style>
